﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using QzEbook.Common;
using System.Web;

/// <summary>
///Tag 的摘要说明
/// </summary>
/// 
namespace QzEbook.AccessObject
{
    public class Tag:abstractAccess<Tag>
    {
        private int id;
        private string name;
        private int count;

        #region 属性;
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public int Count
        {
            get { return count; }
            set { count = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        #endregion

        public Tag()
        {
        }

        protected override Tag bindRow(DataRow row)
        {
            Tag tag = new Tag();
            tag.Id = (Int32)row["TID"];
            tag.Name = row["TName"].ToString();
            tag.Count = (Int32)row["TRefs"];
            return tag;
        }

        /// <summary>
        /// 选取所有和Tag;
        /// </summary>
        /// <returns></returns>
        /// 
        public new List<Tag> selectList()
        {
            if (HttpContext.Current.Cache["TagList"] == null)
            {
                List<Tag> list=base.selectList();
                HttpContext.Current.Cache.Insert("TagList", list, null, DateTime.Now.AddHours(1), TimeSpan.Zero);
            }
            return HttpContext.Current.Cache["TagList"] as List<Tag>;
        }


        protected override DataTable selectTable()
        {
            return this.selectData(-1);
        }


        public List<Tag> SearchTag(string filter)
        {
            DbCommand comand = CreateCommand("SearchTag");
            addParameter(comand, "@name", filter);
            return bindTable(DataAccess.ExcuteSelectCommand(comand));
        }

        public static string getName(int index)
        {
            List<Tag> list = new Tag().selectList();
            foreach (Tag tag in list)
            {
                if (tag.Id == index)
                {
                    return tag.Name;
                }
            }
            return String.Empty;
        }

        /// <summary>
        /// 删除一个tag引用;
        /// </summary>
        /// <returns></returns>
        public bool remove()
        {
            DbCommand command = CreateCommand("RemoveTag");
            addParameter(command, "@TName", Name);
            int i = -1;
            try
            {
                i = DataAccess.ExecuteNonQuery(command);
            }
            catch
            {
            }
            return (i != -1);
        }

        /// <summary>
        /// 插入Tag
        /// </summary>
        /// <returns></returns>
        public override bool insert()
        {
            DbCommand command = CreateCommand("InsertTag");
            addParameter(command, "@TName", Name);
            addParameter(command, "@TID", Id, ParameterDirection.Output);
            int i=-1;
            try{
                i=DataAccess.ExecuteNonQuery(command);
                this.Id = (int)command.Parameters["@TID"].Value;
            }catch{
            }
            return (i != -1);
        }
        /// <summary>
        /// 选取最新20个,Tag
        /// </summary>
        /// <returns></returns>
        public DataTable selectData(int tagSize)
        {
            //tagsize=-1就选取全部;
            DbCommand command = CreateCommand("SelectTag");
            addParameter(command, "@TagSize", tagSize);
            return DataAccess.ExcuteSelectCommand(command);
        }

        public static object getLinkHtml(int tagId)
        {
            return string.Format("<a href=\"list.xhtml?Tid={0}\">{1}</a>", tagId, Tag.getName(tagId));
        }
        public static object getLinkHtml(int tagId,int count)
        {
            return string.Format("<a href=\"list.xhtml?Tid={0}\">{1}({2})</a>", tagId, Tag.getName(tagId),count);
        }
    }
}